// configuration.go
package main

import (
	"log"

	"gorm.io/driver/mysql"
	"gorm.io/gen"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
)

func main() {
	// Initialize the generator with configuration
	g := gen.NewGenerator(gen.Config{
		OutPath:       "./rpc/admin/auth/gen/query", // output directory, default value is ./query
		Mode:          gen.WithDefaultQuery | gen.WithQueryInterface,
		FieldNullable: true,
	})

	// 配置MySQL数据库连接信息
	dsn := "root:root@tcp(127.0.0.1:3306)/zero?charset=utf8mb4&parseTime=True&loc=Local"

	// 连接MySQL数据库
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
		// 禁止外键约束
		DisableForeignKeyConstraintWhenMigrating: true,
		// 为了确保数据一致性，GORM 会在事务里执行写入操作（创建、更新、删除）。如果没有这方面的要求，您可以在初始化时禁用它，这样可以获得60%的性能提升
		SkipDefaultTransaction: true,
		NamingStrategy: schema.NamingStrategy{
			TablePrefix:   "zero_", // 表名前缀
			SingularTable: false,   // 单数表名
			NoLowerCase:   false,   // 关闭小写转换
		},
	})
	if err != nil {
		log.Fatalf("Failed to connect database: %v", err)
	}

	// Use the above `*gorm.DB` instance to initialize the generator,
	// which is required to generate structs from db when using `GenerateModel/GenerateModelAs`
	g.UseDB(db)

	g.ApplyBasic(
		g.GenerateModel("admin_admins"),
	)

	// Execute the generator
	g.Execute()
}
